package com.mmall.concurrency.example.aqs;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

/**
 * <p>Descriptions...
 *
 * @author Diamon.Cheng
 * @date 2019/12/9.
 */
@Slf4j
public class CountDownLatch_2 {
    
    private final static int threadCount = 200;
    
    public static void main(String[] args) throws Exception {
        
        
        ExecutorService exec = Executors.newCachedThreadPool();
        
        final CountDownLatch count = new CountDownLatch(threadCount);
        
        for (int i = 0; i < threadCount; i++) {
            final int threadNum = i;
            exec.execute(() -> {
                try {
                    test(threadNum);
                    
                } catch (Exception e) {
                    log.error("exception", e);
                } finally {
                    count.countDown();
                }
            });
            
        }
        count.await(10, TimeUnit.MILLISECONDS);
        log.info("finish");
        exec.shutdown();
        
    }
    
    private static void test(int threadNum) throws Exception {
        Thread.sleep(100);
        log.info("{}", threadNum);
      
    }
}
